package demo.dubbo.config;

import org.springframework.context.annotation.Bean;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;

public class DubboBaseConfig {
    @Bean
    public RegistryConfig registry() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("127.0.0.1:2181");
        registryConfig.setProtocol("zookeeper");
        return registryConfig;
    }

    @Bean
    public ApplicationConfig application() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("testApp");
        return applicationConfig;
    }

    @Bean
    public MonitorConfig monitorConfig() {
        MonitorConfig mc = new MonitorConfig();
        mc.setProtocol("registry");
        return mc;
    }

    @Bean
    public ReferenceConfig referenceConfig() {
        ReferenceConfig rc = new ReferenceConfig();
        rc.setMonitor(monitorConfig());
        return rc;
    }

    @Bean
    public ProtocolConfig protocalConfig() {
        ProtocolConfig cfg = new ProtocolConfig();
        cfg.setName("dubbo");
        cfg.setPort(20880);
        return cfg;
    }

    @Bean
    public ProviderConfig providerConfig() {
        ProviderConfig cfg = new ProviderConfig();
        // [配置关联]:协议BeanId，可以在<dubbo:service proivder="">中引用此ID
        cfg.setId("");
        // [性能调优] :协议名称
        cfg.setProtocol(protocalConfig());
        // 自动查找本机IP [服务发现] 服务主机名，多网卡选择或指定VIP及域名时使用，为空则自动查找本机IP，建议不要配置，让Dubbo自动获取本机IP
        cfg.setHost(null);
        // [性能调优] 服务线程池大小(固定大小)
        cfg.setThreads(100);
        // 可选 88388608(=8M) [性能调优] 请求及响应数据包大小限制，单位：字节
        cfg.setPayload(88388608);

        cfg.setMonitor(monitorConfig());
        // 延迟发布
        cfg.setRegister(false);
        return cfg;
    }

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig config = new ApplicationConfig();
        config.setName("aa");
        // 禁用或启用QOS服务
        config.setQosEnable(true);
        config.setQosAcceptForeignIp(true);
        config.setQosPort(22222);
        return config;
    }
}
